<template>
  <div class="login_container">
        <div class="login_box">
            <el-form  label-width="0px" :rules="check" :model="loginForm" ref="formRef">
                <h1>商品管理系统</h1>
                <!-- username -->
                <el-form-item class="form" prop="username">
                    <el-input prefix-icon="el-icon-user" v-model="loginForm.username" placeholder="请输入账号"></el-input>
                </el-form-item>
                <!-- passworld -->
                <el-form-item class="form" prop="password">
                    <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class="btn">
                    <el-button type="primary" @click="checkLogin()">登录</el-button>
                    <el-button type="info" @click="reset()">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
  </div>
</template>

<script>
  
    export default {
        name:'Login',
        data(){
            return {
                // 登录数据收集
                loginForm: {
                    username:'admin',
                    password:'123456'
                },
                // 验证规则
                check: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        { min: 3, max: 8, message: '长度在 3 到 8 的用户名', trigger: 'blur' }
                        
                    ],
                    password: [
                        {required: true, message: '请输密码', trigger: 'blur'},
                        { min: 3, max: 10, message: '长度在 3 到 10 位的密码', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            // 重置表单
            reset(){
                this.$refs.formRef.resetFields()
            },
            // 登录验证
            checkLogin() {
                this.$refs.formRef.validate( async val=> {
                    if(!val) return
                    const {data : res } = await this.$http.post('login',this.loginForm)
                    if(res.meta.status !== 200) 
                    return this.$message.error('登陆失败')
                    this.$message.success('登录成功')
                    // 1.将登陆成功后的token保存到客户端的sessionStorage中
                    // 原因：项目中的其他接口必须要在有token的情况下才能被访问
                    //       保证token在当前网站打开期间才有效
                    window.sessionStorage.setItem('token',res.data.token)
                    // 2.通过编程式导航倒转到后台主页，路由地址/home
                    this.$router.push('/home')
                    
                })
            }
        }
    }
</script>

<style scoped> 
    h1 {
        text-align: center;
        padding: 25px 0;
    }

    .login_container {
        background-color: #2b4b6b;
        height: 100vh;
    }

    .login_box {
        background-color: #fff;
        width: 450px;
        height: 300px;
        position: relative;
        left: 35%;
        top: 30%;
        border-radius: 4px;
    }

    .btn {
        display: flex;
        justify-content: end;
    }

    .form {
        width: 80%;
        margin: 25px 10px 25px 45px;
        
    }
</style>